﻿using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;
using Verse.AI;

namespace Miao.Stand
{
    public class OraOraAbility : StandAbility
    {
        public OraOraAbility(StandPawn stand): base(stand)
        {
             
        }
        Command_StandRadiusTarget command_Target;
        public override void Init()
        {
            base.Init();
            command_Target = new Command_StandRadiusTarget(this);
            SetDefField(command_Target);
            command_Target.targetingParams = TargetingParameters.ForAttackAny();
            command_Target.order = 19;
            command_Target.action = ChoseTarget;
            void ChoseTarget(LocalTargetInfo localTargetInfo)//这个是Targeter选中的目标
            {
                var vbs = stand.verbTracker.AllVerbs;
                //Verb v = command_Target.standPawn.TryGetAttackVerb(localTargetInfo.Thing);
                foreach (Verb vb in vbs)//强制调用真的有效果//其实也不太需要优化这种性能
                {
                    if (vb is Verb_MeleeBurstAttackDamage)
                    {
                        Job job = JobMaker.MakeJob(JobDefOf.AttackMelee, localTargetInfo.Thing);
                        job.playerForced = true;
                        job.verbToUse = vb;


                        vb.castCompleteCallback += () =>//游戏本身改动原因，就攻击后才扣除法力值和CD
                        {
                            StartCast();
                            Job ghostFollow = JobMaker.MakeJob(StandJobDefOf.StandGhostFollow);
                            stand.jobs.StartJob(ghostFollow);
                        };
                        //job.expiryInterval = 200;
                        if (localTargetInfo.Pawn != null)
                        {
                            job.killIncappedTarget = localTargetInfo.Pawn.Downed;
                        }
                        stand.jobs.TryTakeOrderedJob(job, JobTag.Misc);//尝试用这个设置攻击
                        return;
                    }
                }
            }
        }

        public override Gizmo DrawGizmo()
        {
            bool disabled = GhostFollow(command_Target) & PowerCostEnough(command_Target)& CooldownComplete(command_Target);
            command_Target.disabled = !disabled;
            return command_Target;
        }
    }
}
